<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/context/mytags.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>上传csv</title>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery/jquery.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/plupload/plupload.full.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery/jquery-ui/jquery-ui.js"></script>

<script src="${pageContext.request.contextPath}/js/bootstrap/bootstrap.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/ui/table/bootstrap-table.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/ui/table/plugins/tableExport.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/ui/table/plugins/table-export.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/ui/table/plugins/jquery.base64.js" type="text/javascript"></script>

<script src="${pageContext.request.contextPath}/ui/layui/layui.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/ui/layui/lay/modules/laydate.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath}/ui/layui/lay/modules/layer.js" type="text/javascript"></script>
 <script src="${pageContext.request.contextPath}/ui/layui/lay/modules/form.js" type="text/javascript"></script>
 <link rel="stylesheet" href="${pageContext.request.contextPath}/ui/layui/css/modules/laydate/laydate.css" >
 <link rel="stylesheet" href="${pageContext.request.contextPath}/css/jquery-css/jquery-ui.css">
 <link rel="stylesheet" href="${pageContext.request.contextPath}/css/bootstrap-css/bootstrap.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/ui/table/bootstrap-table.css">
<link rel="stylesheet" href="${pageContext.request.contextPath}/ui/layui/css/layui.css" >
<link rel="stylesheet" href="${pageContext.request.contextPath}/ui/layui/css/global.css" >
<link rel="stylesheet" href="${pageContext.request.contextPath}/ui/layui/css/modules/layer/default/layer.css" >
 <link rel="stylesheet" href="${pageContext.request.contextPath}/ui/layui/css/modules/code.css" >
 <style>
  
  </style>

</head>
<body>
    

<%@include file="/webpage/frame/top.jsp" %>
<div class="container">

       
        <div id="progressbar" ></div><div id="finish"  style="font-weight:bold;font-size:20px;margin: 10px auto" > <button id="uploader" style="height:40px;width:100px;margin:  10px auto " >选择文件</button>&nbsp;&nbsp;请选择要上传的csv文件</div>
 
		<div class="layui-form-pane" style="margin-top: 15px;">
  			
  			 
      </div>
		
        <div id="result"></div>
  
        <p class="toolbar">
        	
           <button type="button" class="export-excel btn btn-primary btn-xs" data-table="#table">导出Excel</button>
           
        </p>
        <table id="table"
               data-show-refresh="true"
               data-show-columns="true"
               data-search="true"
               data-click-to-select="true"
               data-show-export="true"
               data-query-params="queryParams"
			   data-pagination="true"		   
               data-toolbar=".toolbar"            
               >
            <thead>
            <tr>
            	<th data-field="state" data-radio="true"></th>
            	<th data-field="id" data-show="true">id</th>
                <th data-field="companyName">公司名称</th>
                <th data-field="companyState">公司状态</th>
                <th data-field="registerTime">注册时间</th>
               
                <th data-field="industry">所属行业</th>
            </tr>
            </thead>
        </table>
       
    

    <div id="modal" class="modal fade" style="display:none">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title"></h4>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label>公司名称</label>
                        <input type="text" class="form-control" name="companyName" placeholder="公司名称">
                    </div>
                    <div class="form-group">
                        <label>管理状态</label>
                        <input type="number" class="form-control" name="companyState" placeholder="公司状态">
                    </div>
                    <div class="form-group">
                        <label>注册时间</label>
                        <input type="number" class="form-control" name="registerTime" placeholder="注册时间">
                    </div>
                    <div class="form-group">
                        <label>描述</label>
                        <input type="text" class="form-control" name="description" placeholder="描述">
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary submit">提交</button>
                </div>
            </div>
        </div>
    </div>
	</div>


<!-- 上传组件初始化代码 -->
<script>

    var API_URL = '${pageContext.request.contextPath}/companyController.do?getCompanys';

    var $table = $('#table').bootstrapTable(
    		{
    			url: API_URL,
    			pageSize:20
    		}
    		
    
    );
    $(function () {
    	//获取选中值
    	
    	 TableExport.init();
    	//导出按钮
    	$('#export').click(function () {
    		
    	  	$table.tableExport({
    	      type: 'excel',
    	      escape: false
    	    });
    	  });
        // create event
        

    });

    function queryParams(params) {
        return {};
    }

    
</script>
 <script type="text/javascript">
 //上传公司筛选
 
 	    var companyid="";
 		var accountId = '';
 		var interval_import = '';
 		var upload_click = false;
        var uploader;
        
       
        upload();
        uploader.init();
        
      function getImportMsg(){
    	  var GET_IMPORT_MSG = "${pageContext.request.contextPath}/importStateController.do?getImportStateMsg";
    	  $.ajax({  
  	       	 url: GET_IMPORT_MSG  
  	        , type: "POST"
  	        
  	        , data: {  
  	        	companyId:companyId
  	        }, 
  	        dataType :"json" ,
  	        beforeSend: function(request){  
  	        }, success: function(data, status){
  	        		console.log("返回信息："+data);
  	        		if(data=='0'||data==''){
  	        			$("#finish").html('网络失败');
  	        			$("#finish").append("&nbsp;&nbsp<button id='uploader' style='height:40px;width:150px;margin:  10px auto ' >重新上传csv</button>");
  	        			clearInterval(interval_import);
  	        			upload();
  	        			//uploader.init();
  	        		}
  	        		
  	        	
  	        		if(isContains(data,'导入完成')){
  	        			$("#finish").html(data);
  	        			$("#finish").append("&nbsp;&nbsp<button id='uploader' style='height:40px;width:150px;margin:  10px auto ' >再次上传csv</button>");
  	        			clearInterval(interval_import);
  	        			upload();
  	        			//uploader.init();
  	        		}else if(isContains(data,'导入失败')){
  	        			$("#finish").html(data);
  	        			$("#finish").append("&nbsp;&nbsp<button id='uploader' style='height:40px;width:150px;margin:  10px auto ' >重新上传csv</button>");
  	        			clearInterval(interval_import);
  	        			
  	        			//uploader.init();
  	        		}else{
  	        			$("#finish").html(data);
  	        			
  	        		}
  	        		
  	        	
  	        		
  	        		
  	        }, error: function(request, error, status){  
  	            // todo: 错误处理  
  	            
  	        }, complete: function(request, error, status){  
  	        }  
  	    });  
    	  
      }
      
      function upload(){
    	  uploader = new plupload.Uploader({
    		  runtimes : 'html5,flash,silverlight,html4',//设置运行环境，会按设置的顺序，可以选择的值有html5,gears,flash,silverlight,browserplus,html
              flash_swf_url : './js/plupload/Moxie.swf',
              silverlight_xap_url : './js/plupload/Moxie.xap',
              url : '${pageContext.request.contextPath}/uploadFile.do?companyId='+companyid,//上传文件路径
              max_file_size : '10mb',//100b, 10kb, 10mb, 1gb
              chunk_size : '1mb',//分块大小，小于这个大小的不分块
              unique_names : true,//生成唯一文件名
              browse_button : 'uploader', 
              filters: [{title: "Custom Files",extensions: "csv"}],//图片限制
             
              init : {
            	 
              	FileFiltered:function(up,file){
              		$("#finish").html("请选择正确的文件类型")
              	},
                  FilesAdded: function(up, files) {
                	 
                      uploader.start();
                      $("#finish").html("开始上传")
                     
                  },
                  FileUploaded : function(up, file, info) {//文件上传完毕触发
                  	 var response = $.parseJSON(info.response);
                     
                    
                      if (response.status) {
                      	 $("#finish").html(response.message);
                          //$('#result').append( $('<div> "文件路径是："' + response.fileUrl + '"随机的文件名字为："' + file.name + '</div>') );
                      }else{
                      	 $("#finish").html(response.message);
                      }
                  },
                  UploadComplete : function( uploader,files ) {
                	    uploader.destroy();
                  		interval_import = setInterval("getImportMsg()",50);
                     		
                  },
                  UploadProgress : function( uploader,file ) {
                     // $("#progress").html("上传进度为：" + file.percent + "%");
                     
                     $( "#progressbar" ).progressbar({
        					value:  file.percent
        			 });
                     
                  },
                  Error: function(up, args) {
                      // Called when error occurs
                      if(args.code==-601){
                    	  $("#finish").html("<font color='red'>只允许上传csv文件</font><button id='uploader' style='height:40px;width:150px;margin:  10px auto ' >再次上传csv</button>")	  
                      }
                     
                  }
              }
          });
    	  
    	 
    	  
    
      }
      
      
      
      function isContains(str, substr) {
    	    return str.indexOf(substr) >= 0;
    	}
      
      function getAccount(){
    	  if(upload_click){
  	    	console.log(upload_click);
  	    	return true;
  	     }
    	  var obj = eval($table.bootstrapTable('getSelections'));
    	  companyId=obj[0].id;
    	  accountId= '0';
    	  var GET_ACCOUNT_URL = '${pageContext.request.contextPath}/companyController.do?checkAccount'
    	  $.ajax({  
    	        url: GET_ACCOUNT_URL  
    	        , type: "POST"
    	        , async:true
    	        , data: {  
    	        	companyId:companyId
    	        }, 
    	        dataType :"json" ,
    	        beforeSend: function(request){  
    	        }, success: function(data, status){
    	        	
    	        	accountId= data;
    	        	console.log(data);
    	        	if(accountId!='0'){
    	        		console.log($(".moxie-shim-html5").find('input[type=file]'));
    	        		
        	        	uploader.settings.url = '${pageContext.request.contextPath}/uploadFile.do?accountId='+accountId+'&companyId='+companyId
    	        		   	        		    	        	      	        		    	        		
    	        	}else{
    	        		alert('此公司面没有添加百度账号');
    	        		return false;
    	        	}
    	        		return true;
    	        		
    	        }, error: function(request, error, status){  
    	            // todo: 错误处理  
    	        }, complete: function(request, error, status){  
    	        }  
    	    });  
    	  
    	 	return false;
      }
      
      $(
    	
    	function(){
    		$("#uploader").mouseover(
    			function(){
    				if($(".bs-checkbox input:radio[name='btSelectItem']:checked").val()==undefined){
    					alert("请选择一间公司");
    				    //uploader.destroy();	
    					return  false;
    				}else{
    					
    					getAccount();
    			 	}
    			}
    		);
    		 
    	}
    	
      );
      

      
</script>    



<%@include file="/webpage/frame/bottom.jsp" %>
</body>

</html>